package com.scf.loanmanage.controller;

import com.scf.loanmanage.configuration.RabbitmqUtil;
import com.scf.loanmanage.dao.RepaymentMapper;
import com.scf.loanmanage.dao.RepaymentRegisteredMapper;
import com.scf.loanmanage.entity.LoanLevelFive;
import com.scf.loanmanage.entity.PageBean;
import com.scf.loanmanage.entity.Repayment;
import com.scf.loanmanage.entity.RepaymentRegistered;
import com.scf.loanmanage.rabbitmq.BusinessSender;
import com.scf.loanmanage.service.ServiceImp.LoanLevelFiveImpl;
import com.scf.loanmanage.service.ServiceImp.RepaymentRegisteredImpl;
import com.scf.uitl.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/RepaymentRegistered")
public class RepaymentRegisteredController {
    @Resource
    RabbitmqUtil rabbitmqUtil;
    @Resource
    RepaymentRegisteredImpl repaymentRegistered;
    @Resource
    RepaymentMapper repaymentMapper;
    @Resource
    RepaymentRegisteredMapper repaymentRegisteredMapper;
    @Resource
    LoanLevelFiveImpl loanLevelFives;

    @Resource
    BusinessSender businessSender;

        //分页还款记录
    @GetMapping("/RepaymentRegisteredSelectall")
    public ResponseResult selectall(Integer reid,Integer pageIndex, Integer pageSize){
        System.out.println("reid:"+reid);
        ResponseResult responseResult=new ResponseResult();
        List<RepaymentRegistered> repaymentRegistereds = repaymentRegistered.selectAll(reid, pageIndex, pageSize);
        Integer selectcount = repaymentRegistered.selectcount();

        PageBean<RepaymentRegistered> pageBean=new PageBean<>();
        pageBean.setBeanList(repaymentRegistereds);

        pageBean.setPageBegin(pageIndex);
        pageBean.setPageEnd(pageSize);
        pageBean.setTotalRecord(selectcount);
        responseResult.setData(pageBean);
        return responseResult;
    }
    @GetMapping("/RepaymentReid")
    public Repayment se(Integer reid){
        Repayment repayment = repaymentMapper.selectByReid(reid);

        return repayment;
    }

    @GetMapping("/insert")
    public void ses(Date repaymentdate, Double overdue, Double repaymentstatus, Double shouldprincipal, Integer reid, Double shouldtotal){
        Repayment repayment = repaymentMapper.selectByReid(reid);
        System.out.println(reid+"有的"+"term--"+repayment.getTerm()+"enterprisename---"+repayment.getEnterprisename());
//        repaymentRegisteredMapper.insertByreid(null, repaymentdate, new Date(), shouldtotal,shouldprincipal, overdue, 10, repaymentstatus,"已还清",reid);
        Double Interestpayable = repaymentRegistered.selectReidzongjine(reid);
        Double exInterest = repaymentRegistered.selectReidzonglixi(reid);
        System.out.println("要还 的金额是："+Interestpayable+"利息是--"+exInterest);
        //修改还款
//        repaymentMapper.update(reid,Interestpayable,exInterest);

        Double term = repayment.getTerm();
        Double myDouble = Double.valueOf(term);
        double dbl = myDouble.doubleValue();
        int intgr = (int) dbl;
        Integer val = Integer.valueOf(intgr);
        LoanLevelFive loanLevelFive=new LoanLevelFive();
        System.out.println("执行到这----");
        loanLevelFive.setBorrowedterm(val);
        loanLevelFive.setCode(repayment.getCode());
        loanLevelFive.setEnterprisename(repayment.getEnterprisename());
        loanLevelFive.setAmountborrowed(repayment.getBorrowed());
        //五级分类
        System.out.println("之前loanLevelFive--"+loanLevelFive);
        loanLevelFives.insertFive(loanLevelFive);
        System.out.println("之后loanLevelFive--"+loanLevelFive);
    }




    @GetMapping("/Timer")
    public void setTimer(String date2,Integer reid,Double money) {
        System.out.println("日期----"+"-----"+date2+"Reid是-"+reid);
        String date = date2;
        date = date.replace("Z", " UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
//         Date d = null;
        try {
          final  Date   d = format.parse(date);
            System.out.println("转换后"+d);
            int l = (int) ((int)d.getTime() - new Date().getTime());
            RabbitmqUtil rabbitmqUtil=new RabbitmqUtil();
            rabbitmqUtil.setD(d);
            rabbitmqUtil.setMoney(money);
            rabbitmqUtil.setReid(reid);
            businessSender.send(l,rabbitmqUtil);

//            Timer timer=new Timer();
//            timer.schedule(new TimerTask() {
//                @Override
//                public void run() {
//                    System.out.println("11111----预定执行还款");
//                    Repayment repayment = repaymentMapper.selectByReid(reid);
//                    repaymentRegisteredMapper.insertByreid(null, d, null,repayment.getBorrowed(),money, 500.00, 10, 0.0,"已还清",reid);
//                    Double Interestpayable = repaymentRegistered.selectReidzongjine(reid);
//                    Double exInterest = repaymentRegistered.selectReidzonglixi(reid);
//                    System.out.println("要还 的金额是："+Interestpayable+"利息是--"+exInterest);
//                    repaymentMapper.update(reid,Interestpayable,exInterest);
//                }
//            },d );
        } catch (ParseException e) {
            e.printStackTrace();
        }




    }
}
